package cn.fl.system.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.dto.SysBkInfoDTO;
import cn.fl.system.service.ISysBkInfoApiService;
import cn.fl.system.service.ISysBkInfoService;
import cn.fl.system.transform.SysBkInfoVOTransform;
import cn.fl.system.vo.SysBkInfoVO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;

@Service
public class SysBkInfoServiceImpl implements ISysBkInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysBkInfoServiceImpl.class);
	
	/**
	 * 系统银行信息API暴露服务
	 */
	@Autowired
	private ISysBkInfoApiService sysBkInfoApiService;

	@Override
	public SysBkInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "根据主键查询银行信息失败,主键参数为空.ID:"+id,CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		SysBkInfoDTO dto = this.sysBkInfoApiService.selectSysBkInfoById(id,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		return SysBkInfoVOTransform.toVO(dto);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.sysBkInfoApiService.searchSysBkInfoListPage(page,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		List<SysBkInfoDTO> resultList = (List<SysBkInfoDTO>) page.getResultList();
		page.setResultList(SysBkInfoVOTransform.toVOList(resultList));
		return page;
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException {
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除银行失败,主键参数为空.IDS:"+JSONObject.toJSONString(ids),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return sysBkInfoApiService.removeSysBkInfoByIds(ids,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public Long add(SysBkInfoVO entity) throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增银行失败,银行对象参数为空.DATA:"+JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.sysBkInfoApiService.addSysBkInfo(SysBkInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean modify(SysBkInfoVO entity) throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改银行失败,银行对象参数为空.DATA:"+JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysBkInfoApiService.modifySysBkInfo(SysBkInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean enAbleSysBkInfo(Long id) throws BaseException {
		if(null == id){
			BaseLogUtils.info(logger, "enAbleSysBkInfo", "启用银行失败,主键参数为空.ID:"+id,CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysBkInfoApiService.enAbleSysBkInfo(id,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public boolean disAbleSysBkInfo(Long id) throws BaseException {
		if(null == id){
			BaseLogUtils.info(logger, "disAbleSysBkInfo", "禁用银行失败,主键参数为空.ID:"+id,CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysBkInfoApiService.disAbleSysBkInfo(id,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
	}
	//狮桥自有账户审核流程 by gengchunlan 20180716
	@Override
	public DataResultDTO addFlowStartOwnAccount(FlowStartParamsDTO flowStartParamsDTO) throws BaseException {
		if (StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())) {
			BaseLogUtils.info(logger, "addFlowStartOwnAccount", "用户ID和业务数据主键必须要存在!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return this.sysBkInfoApiService.addFlowStartOwnAccount(flowStartParamsDTO);
	}
	
}
